package com.winenine999.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.custom.dao.CWinebrandMapper;
import com.winenine999.model.Page;
import com.winenine999.model.Syuser;
import com.winenine999.model.Syuserrole;
import com.winenine999.model.Winebrand;
import com.winenine999.model.Winery;
import com.winenine999.model.Winetype;
import com.winenine999.service.ISyuserService;
import com.winenine999.service.ISyuserroleService;
import com.winenine999.service.IWinebrandService;
import com.winenine999.service.IWineproductService;
import com.winenine999.service.IWineryService;
import com.winenine999.service.IWinetypeService;

@Controller
@RequestMapping(value="/syuser")
public class SyuserController {
	@Autowired
	private IWineryService wineryService;
	@Autowired
	private IWinetypeService winetypeService;
	@Autowired
	private IWineproductService wineproductService;
	@Autowired
	private IWinebrandService winebrandService;
	@Autowired
	private ISyuserService syuserService;
	@Autowired
	private ISyuserroleService syuserroleService;
	/**
	 * 进入syuser用户主页面
	 * @return
	 */
	@RequestMapping(value="/syuser",method=RequestMethod.GET)
	public String syuser(){
		return "permission/syuser";
	}
	/**
	 * 分页查询syusers，给datatables返回值
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value="/syuser_list_datatables",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> syuser_list_datatables(Integer start,Integer length){
		Map<String,Object> map=new HashMap<String,Object>();
		Page page1=syuserService.selectPageUsersAndZWName(new Page(start,length));
		map.put("aaData", page1.getList());//查到的结果集
		map.put("recordsFiltered", page1.getTotal());//总条数，用于计算页数
		map.put("recordsTotal",  page1.getTotal());//总条数，用于页面显示：‘全部记录数  条’
		return map;
	}
	/**
	 * 添加syuser
	 * @param syuser
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add(	Syuser syuser){
		Integer result=syuserService.insertSelective(syuser);
		return result;
	}
	/**
	 * 通过id拿到修改的数据填充修改弹框
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/updatePre",method=RequestMethod.POST)
	@ResponseBody
	public Syuser updatePre(Integer id){
		Syuser syuser=syuserService.selectByPrimaryKey(id);
		return syuser;
	}
	/**
	 * 修改syuser
	 * @param syuser
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Integer update(Syuser syuser){
		Integer result=syuserService.updateByPrimaryKeySelective(syuser);
		return result;
	}
	/**
	 * 根据id删除syuser
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		
		result=syuserService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 查询该用户所具有的角色和不具有的角色
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/grant",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> grant(Integer id){
		List<Map<String,Object>> maps=syuserService.findUserAndRole(id);
		return maps;
	}
	/**
	 * 1、通过用户id删除该用户的所有角色
	 * 2、在将checkbox上被勾选的角色插入到 user-role表中
	 * @param id
	 * @param roleIds
	 * @return
	 */
	@RequestMapping(value="/grantRole",method=RequestMethod.POST)
	@ResponseBody
	public Integer grantRole(Integer id,Integer[] roleIds){
		System.out.println("id:"+id);
		syuserroleService.deleteUserRoles(id);
		if(roleIds!=null &&roleIds.length>0){
			
			for(int i=0;i<roleIds.length;i++){
				Syuserrole syuserrole=new Syuserrole ();
				syuserrole.setRoleid(roleIds[i]);
				syuserrole.setUserid(id);
				System.out.println("roleIds[i]:"+roleIds[i]);
				syuserroleService.insertSelective(syuserrole);
			}
		}
		return 0;
	}
}
